home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VGATUT3.ZIP / VGAPALL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-24  |  4.7 KB  |  192 lines

  1. #include "..\include\vgafunc.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6. void DrawScreen(void);
  7. void Intro(void);
  8. void Outro(void);
  9. void FadeNRotate(unsigned char *, unsigned char *);
  10.  
  11. unsigned char *Screen = (unsigned char *)0xA0000000;
  12.  
  13. // ──────────────────────────────
  14. // VGA Tutorial III - The Palette
  15. //
  16. // Barny Mercer - 1995
  17. // ──────────────────────────────
  18.  
  19. void main(void)
  20. {
  21.     // gradient array - fade from black to red to yellow to white to black
  22.     GradPal BarsData[5] = { {  0,  0,  0,   0},
  23.                 { 63,  0,  0,  64},
  24.                 { 63, 63,  0, 128},
  25.                 { 63, 63, 63, 192},
  26.                 {  0,  0,  0, 255},
  27.               };
  28.  
  29.     static unsigned char BarsPal[768];
  30.  
  31.     // black to blue to white
  32.     GradPal BlueSkyData[4] ={ {     0,  0,  0,   0},
  33.                   {  0,  0, 63,  32},
  34.                   { 63, 63, 63,  96},
  35.                   {     0,  0,     0, 255},
  36.                 };
  37.  
  38.     static unsigned char BlueSkyPal[768];
  39.  
  40.     // ZEBRA
  41.     // black to white to black..... (you get the idea)
  42.     GradPal ZebraData[11] ={ {    0,  0,    0,   0},
  43.                  { 63, 63, 63,  25},
  44.                  {    0,  0,    0,  50},
  45.                  { 63, 63, 63,  75},
  46.                  {    0,  0,    0, 100},
  47.                  { 63, 63, 63, 125},
  48.                  {    0,  0,    0, 150},
  49.                  { 63, 63, 63, 175},
  50.                  {    0,  0,    0, 200},
  51.                  { 63, 63, 63, 225},
  52.                  {    0,  0,    0, 255},
  53.                };
  54.  
  55.     static unsigned char ZebraPal[768];
  56.     static unsigned char CurPal[768] = {0};    // current working palette
  57.  
  58.     // create new palettes to be used later
  59.     MakeGradPal( BarsData, 5, BarsPal );
  60.     MakeGradPal( BlueSkyData, 4, BlueSkyPal );
  61.     MakeGradPal( ZebraData, 11, ZebraPal );
  62.  
  63.     // say 'Hello' to all the boys & girls
  64.     VidMode( VID_TEXT );
  65.     Intro();
  66.  
  67.     VidMode( VID_320x200 );
  68.  
  69.     // set entire palette to black so that we can draw on the screen
  70.     // without being seen
  71.     SetPalToBlack();
  72.     DrawScreen();
  73.  
  74.     FadeNRotate( CurPal, ZebraPal );
  75.  
  76.     // morph palettes
  77.     FadePalToPal( ZebraPal, BlueSkyPal );
  78.  
  79.     // clear keyboard buffer
  80.     getch();
  81.  
  82.     // cycle palette
  83.     while (!_kbhit())
  84.     {
  85.     PalCycle( BlueSkyPal, -1, 0, 254 );
  86.     SetAllPalette( BlueSkyPal );
  87.     }
  88.  
  89.     // morph palettes again
  90.     FadePalToPal( BlueSkyPal, BarsPal );
  91.  
  92.     // cycle palette fo a while....
  93.     for (int Tmp=0; Tmp<200; Tmp++)
  94.     {
  95.     PalCycle( BarsPal, 1, 0, 254);
  96.     SetAllPalette( BarsPal );
  97.     }
  98.  
  99.     // begin fade out
  100.     for (Tmp=0; Tmp<128; Tmp++)
  101.     {
  102.     // give palette a twist
  103.     PalCycle( BarsPal, 1, 0, 254);
  104.  
  105.     // fade palette closer to correct value
  106.     FadePalToBlack( BarsPal, 1 );
  107.  
  108.     // plunk palette down
  109.     SetAllPalette( BarsPal );
  110.     }
  111.  
  112.     getch();
  113.  
  114.     // reset video mode
  115.     VidMode( VID_TEXT );
  116.     Outro();
  117. }
  118.  
  119. void DrawScreen(void)
  120. {
  121.     float nColour = 1;
  122.  
  123.     for (int Tmp=0; Tmp<320; Tmp++)
  124.     {
  125.     DrawLine( 160, 100, Tmp, 0, (int)(nColour += 0.246), Screen );
  126.     if (nColour>255)
  127.         nColour=1;
  128.     }
  129.  
  130.     for (Tmp=0; Tmp<200; Tmp++)
  131.     {
  132.     DrawLine( 160, 100, 319, Tmp, (int)(nColour += 0.246), Screen );
  133.     if (nColour>255)
  134.         nColour=1;
  135.     }
  136.  
  137.     for (Tmp=319; Tmp>=0; Tmp--)
  138.     {
  139.     DrawLine( 160, 100, Tmp, 199, (int)(nColour += 0.246), Screen );
  140.     if (nColour>255)
  141.         nColour=1;
  142.     }
  143.  
  144.     for (Tmp=199; Tmp>=0; Tmp--)
  145.     {
  146.     DrawLine( 160, 100, 0, Tmp, (int)(nColour += 0.246), Screen );
  147.     if (nColour>255)
  148.         nColour=1;
  149.     }
  150. }
  151.  
  152. void Intro(void)
  153. {
  154.     printf( "Hi there and welcome to this decidedly belated VGA programming tutorial\n");
  155.     printf( "This tutorial concerns it's self with the VGA palette.\n\n" );
  156.     printf( "This program illustrates several palette manipulations.\n" );
  157.     printf( "Have fun......" );
  158.  
  159.     getch();
  160. }
  161.  
  162. void Outro(void)
  163. {
  164.     printf( "Now that certainly looks much nicer than anything we've done so far :)\n\n" );
  165.     printf( "Keep an eye out for tutorial IV which will focus on drawing filled polygons\n\n");
  166.     printf( "Happy coding...... \n\n" );
  167.     printf( "Barny Mercer :  barny.mercer@zetnet.co.uk " );
  168.  
  169.     getch();
  170. }
  171.  
  172. // this bit fades in the screen while rotating the palette to give a nice
  173. // intro.  Notice that the fade isn't very smooth.  Using the method
  174. // described in the tutorial will make things nicer.
  175.  
  176. void FadeNRotate(unsigned char *CurPal, unsigned char *ZebraPal)
  177. {
  178.     while( !_kbhit() )
  179.     {
  180.     // give palettes a twist
  181.     PalCycle( CurPal, 1, 0, 254);
  182.     PalCycle( ZebraPal, 1, 0, 254 );    // we must cycle zebrapal too
  183.                         // so that the colours stay in line
  184.     // fade palette closer to correct value
  185.     FadePalFromBlack( CurPal, ZebraPal, 1 );
  186.  
  187.     // plunk palette down
  188.     WaitVerticalRetrace();        // wait for a vertical screen retrace to end
  189.     SetAllPalette( CurPal );
  190.     }
  191. }
  192.